# -*- coding:utf-8 -*-
# @FileName :conversion.py
# @Date: 2024/8/3
# @Author:天空之城
import re
import json
import pandas as pd

from address_normalizer.app.config import *


def convert_regions_to_patterns(input_json, output_file):
    """
    读取中国地区 JSON 文件并转换为特定格式的输出。

    :param input_json: 输入的 JSON 文件路径
    :param output_file: 输出文件路径
    """
    # List of municipalities to exclude
    municipalities = ["北京市", "上海市", "天津市", "重庆市"]

    with open(input_json, 'r', encoding='utf-8') as file:
        data = json.load(file)

    output = []

    for prov, cities in data.items():
        if prov in municipalities:
            pass
        else:
            output.append(f"{{'label': 'province', 'pattern': '{prov}'}}")
            to_replace = str(prov).replace('省', '').replace('市', '')
            output.append(f"{{'label': 'province', 'pattern': '{to_replace}'}}")
        for city, districts in cities.items():
            output.append(f"{{'label': 'city', 'pattern': '{city}'}}")
            to_replace = str(city).replace('省', '').replace('市', '').replace('区', '').replace('县', '')
            if len(to_replace) >= 2:
                output.append(f"{{'label': 'city', 'pattern': '{to_replace}'}}")
            for district in districts:
                output.append(f"{{'label': 'district', 'pattern': '{district}'}}")
                to_replace = str(district).replace('省', '').replace('市', '').replace('区', '').replace('县', '')
                if len(to_replace) >= 2:
                    output.append(f"{{'label': 'district', 'pattern': '{to_replace}'}}")
    with open(output_file, 'w', encoding='utf-8') as f:
        f.write('\n'.join(output))


def create_jieba_dict(data, output_file='dictionary.txt', default_frequency=100):
    """
    将输入的数据转换为 Jieba 自定义词典格式并保存到文件。

    :param data: 输入的字典列表，每个字典包含 'label' 和 'pattern' 字段
    :param output_file: 输出的词典文件名
    :param default_frequency: 默认词频
    """
    default_frequency = default_frequency * 10000
    with open(output_file, 'w', encoding='utf-8') as f:
        written_lines = set()  # 存储已写入的行

        for item in data:
            item = item.replace("'", '"')
            item = json.loads(item)
            word = item['pattern']
            freq = default_frequency if default_frequency >= 10000 else 10000
            tag = item['label']
            line = f"{word} {freq} {tag}"
            line_without_numbers = re.sub(r'\d+', '', line)  # 去除数字部分
            if line_without_numbers not in written_lines:  # 检查是否已经写入过该行
                f.write(line + '\n')
                written_lines.add(line_without_numbers)

            to_replace = str(word).replace('省', '').replace('市', '').replace('区', '')
            if len(to_replace) >= 2:
                freq = int(freq)
                freq = freq * 0.95 if freq * 0.95 >= 1000 else 1000
                line = f"{to_replace} {int(freq)} {tag}"
                line_without_numbers = re.sub(r'\d+', '', line)  # 去除数字部分
                if line_without_numbers not in written_lines:  # 检查是否已经写入过该行
                    f.write(line + '\n')
                    written_lines.add(line_without_numbers)

    print(f"词典已保存到 {output_file}")


def load_data_from_json(file_path):
    """
    从 JSON 文件中加载数据。

    :param file_path: JSON 文件路径
    :return: 加载的数据列表
    """
    with open(file_path, 'r', encoding='utf-8') as f:
        data = f.read().split('\n')
    return data


def organize_area_data(data, output_file):
    result = {}
    province = None
    city = None
    for code, name in data.items():
        code = str(code)
        if code.endswith("0000"):
            province = name
            city = province
            result[province] = {province: []}
        elif code.endswith("00"):
            city = name
            result[province][city] = []
        else:
            result[province][city].append(name)

    # 剔除空列表的
    def remove_empty_lists(d):
        if isinstance(d, dict):
            return {k: remove_empty_lists(v) for k, v in d.items() if v}
        elif isinstance(d, list):
            return [remove_empty_lists(i) for i in d if i]
        else:
            return d

    result = remove_empty_lists(result)
    # 将结果写入文件
    with open(output_file, 'w', encoding='utf-8') as f:
        json.dump(result, f, ensure_ascii=False, indent=4)
    return result


def read_area_data(file_path):
    res = {}
    # 读取Excel文件
    df = pd.read_excel(file_path)

    # 获取行政区域代码和单位名称列
    code_column = df['行政区域代码']
    name_column = df['单位名称']

    # 遍历每一行数据
    for i in range(len(df)):
        code = code_column[i]
        name = name_column[i]
        res[code] = name
    return res


def save_province_city_district(data, filename):
    # 创建一个空的DataFrame
    df = pd.DataFrame(columns=['省', '市', '区'])

    # 遍历数据，并将数据添加到DataFrame中
    for province, cities in data.items():
        for city, districts in cities.items():
            for district in districts:
                df = pd.concat([df, pd.DataFrame({'省': [province], '市': [city], '区': [district]})],
                               ignore_index=True)

    # 将DataFrame保存为csv文件
    df.to_csv(filename, index=False)


def process_and_save_area_data(input_path, output_path, json_output_path):
    """
    读取区域数据，组织成所需格式，并保存到CSV文件。

    参数:
    input_path (str): 输入数据文件的路径。
    output_path (str): 输出CSV文件的路径。
    """
    # 读取区域数据
    data = read_area_data(input_path)

    # 组织数据
    organized_data = organize_area_data(data, json_output_path)

    # 保存数据到CSV
    save_province_city_district(organized_data, output_path)


if __name__ == '__main__':
    process_and_save_area_data(EXCEL_FILE, PROVINCE_CITY_DISTRICT, CHINA_REGIONS_JSON)
    convert_regions_to_patterns(CHINA_REGIONS_JSON, PATTERN_FILE)
    create_jieba_dict(load_data_from_json(PATTERN_FILE), output_file=JIEBA_FILE)
